(() => {
	let ele = {};
	let boxs = $('.box');
	
	ele.todayDiceNum = $('#todayDiceNum');//今日可领取骰子数量
	ele.luxury = $('#luxury');//豪华值
	ele.myDice = $('#myDice');//我的骰子
	ele.record = $('#record'); //打开获奖记录
	ele.modal_record = $('#modal-record'); //获奖记录弹框
	ele.close_record = $('#close-record'); //关闭获奖记录弹框
	ele.modal_explian = $('#modal-explian'); //玩法说明
	ele.explian = $('#explian'); //打开玩法说明
	ele.close_explian = $('#close-explian'); //关闭玩法说明
	ele.modal_explian2 = $('#modal-explian2');//提现展示
	ele.explian2 = $('#explian2');//打开提示弹框
	ele.close_explian2 = $('#close-explian2');//关闭提现展示
	ele.dangqianhao = $('#dangqianhao');//打开提示弹框
	ele.nowsite = $('#nowsite');
	ele.touzhi = $('#touzhi');
	ele.xiaohao = $('#xiaohao');
	ele.recordDataList = $('#recordDataList');//获奖记录
	ele.more = $('#more');//获奖记录
	ele.empty = $('#empty');//获奖记录
	ele.getDice = $('#getDice');//领取骰子按钮
	ele.modal_transfer = $('#modal-transfer')//领取提示
	ele.close_transfer = $('#close-transfer')//关闭领取提示
	ele.transferboxinfo = $('#transferboxinfo')//领取提示的列表
	ele.haohuaNum = $('#haohuaNum')//领取骰子弹框的豪华值
	ele.tuozi = $('#tuozi');//确认投掷骰子
	ele.modal_tips2 = $("#modal-tips2");
	ele.close_tips2 = $("#close-tips2");
	ele.recieveimg = $("#recieveimg");
	ele.tipsbtn = $("#tipsbtn");
	ele.recievenum = $("#recievenum");
	ele.xianwan = $("#xianwan");
	ele.zanwei = $(".zanwei");
	ele.mingzi = $("#mingzi");
	ele.recieveimg2 = $("#recieveimg2");
	ele.floimg = $("#floimg");
	ele.myJifen = $('#myJifen');
	
	
	let page = 1;

	// console.log(window.screen.availHeight,window.screen.availWidth);

	let screenWidth = window.screen.availWidth;
	let screenHeight = window.screen.availHeight;

	let nowTop = '';
	let nowLeft = '';
	let nowWidth = '';
	let nowHeight = '';
	
	let oldTop = '';
	let oldLeft = '';
	let oldWidth = '';
	let oldHeight = '';

	let nowid = 1;
	
	let top = '';
	let left = '';
	

	// 打开提现展示
	ele.dangqianhao.onclick = () => {
		ele.modal_explian2.style.display = 'block'
	}
	// 关闭提现展示
	ele.close_explian2.onclick = () => {
		ele.modal_explian2.style.display = 'none'
	}
	//打开 领取记录
	ele.record.onclick = () => {
		openMdl(ele.modal_record);
	}
	//关闭 领取记录
	ele.close_record.onclick = () => {
		closeMdl(ele.modal_record);
	}

	//打开 玩法 说明
	ele.explian.onclick = () => {
		openMdl(ele.modal_explian);
	}
	//关闭 玩法 说明
	ele.close_explian.onclick = () => {
		closeMdl(ele.modal_explian);
	}
	
	// 打开领取骰子
	ele.getDice.onclick = () => {
		getList();
	}
	// 关闭领取骰子
	ele.close_transfer.onclick = () => {
		ele.modal_transfer.style.display = 'none'
	}
	//关闭 恭喜获得
	ele.close_tips2.onclick = () => {
	  closeMdl(ele.modal_tips2);
	};
	ele.tipsbtn.onclick = () => {
	  closeMdl(ele.modal_tips2);
	};
	//投掷骰子
	let jia = 1;
	
	let xz_time = 1;
	ele.touzhi.onclick = () => {
		if(xz_time == 1) {
			landlordGetBu();
			xz_time = 0;
			setTimeout(()=> {
				xz_time = 1;
			},2000)
		} else {
			openTips('请不要频繁点击哦~')
		}
		// let randomNum = Math.floor(Math.random() * 5) + 1;
		// console.log(randomNum);
		// var tis = null;
		// let i = jia;
		// tis = setInterval(()=> {
		// 	position(i++);
		// 	if(i >= jia + randomNum) {
		// 		clearInterval(tis)
		// 		jia = jia + randomNum;
		// 	}
		// },400)
	}
	
	var time = null;
	let every = '';
	
	function position(id) {
		// let newid = (nowid + id) % 24;
		let newid = (id % 24) + 1;
		// console.log(newid);
		let newbox = $('.site' + newid);
		console.log(id,newid,newbox);
		let newboxSite = newbox[0].childNodes[0].parentNode;
		nowTop = newboxSite.getBoundingClientRect().top;
		nowLeft = newboxSite.getBoundingClientRect().left;
		nowWidth = newboxSite.getBoundingClientRect().width;
		nowHeight = newboxSite.getBoundingClientRect().height;
		// if (newboxSite.className.split('site')[1] != 1 && newboxSite.className.split('site')[1] != 7 && newboxSite.className
		// 	.split('site')[1] != 13 && newboxSite.className.split('site')[1] != 19) {
		// 	ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 25 + 'px';
		// 	ele.nowsite.style.left = parseInt(nowLeft) + 14 + 'px';
		// } else {
		// 	ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 15 + 'px';
		// 	ele.nowsite.style.left = parseInt(nowLeft) + 24 + 'px';
		// }
		// frame(nowTop,nowLeft);
		every = 1;
		top = nowTop - oldTop;
		left = nowLeft - oldLeft;
		time = setInterval(()=> {
			frame(newboxSite)
		},10);
	}
	
	
	function frame(newboxSite) {
		if(every > 8) {
			clearInterval(time);
			oldTop = newboxSite.getBoundingClientRect().top;
			oldLeft = newboxSite.getBoundingClientRect().left;
			if (newboxSite.className.split('site')[1] != 1 && newboxSite.className.split('site')[1] != 7 && newboxSite.className
				.split('site')[1] != 13 && newboxSite.className.split('site')[1] != 19) {
				ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 25 + 'px';
				ele.nowsite.style.left = parseInt(nowLeft) + 14 + 'px';
			} else {
				ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 15 + 'px';
				ele.nowsite.style.left = parseInt(nowLeft) + 24 + 'px';
			}
		}
		else {
			every++;
			let topeach = top/10;
			let lefteach = left/10;
			ele.nowsite.style.top = parseInt(ele.nowsite.style.top) + parseInt(topeach) + 'px';
			ele.nowsite.style.left = parseInt(ele.nowsite.style.left) + parseInt(lefteach) + 'px'; 
		}
	}
	
	
	// 初始位置
	function oldposition(id) {
		let newid = id % 25;
		// console.log(id,newid);
		let newbox = $('.site' + newid);
		let newboxSite = newbox[0].childNodes[0].parentNode;
		oldTop = newboxSite.getBoundingClientRect().top;
		oldLeft = newboxSite.getBoundingClientRect().left;
		oldWidth = newboxSite.getBoundingClientRect().width;
		oldHeight = newboxSite.getBoundingClientRect().height;
		if (newboxSite.className.split('site')[1] != 1 && newboxSite.className.split('site')[1] != 7 && newboxSite.className.split('site')[1] != 13 && newboxSite.className.split('site')[1] != 19) {
			ele.nowsite.style.top = parseInt(oldTop) - parseInt(oldHeight) - 25 + 'px';
			ele.nowsite.style.left = parseInt(oldLeft) + 14 + 'px';
		} else {
			ele.nowsite.style.top = parseInt(oldTop) - parseInt(oldHeight) - 15 + 'px';
			ele.nowsite.style.left = parseInt(oldLeft) + 24 + 'px';
		}
	}
	
	
	// 点哪个 跳到哪个box
	// for (let i = 0; i < boxs.length; i++) {
	// 	boxs[i].onclick = (e) => {
	// 		// console.log(boxs[i].className.split('site')[1]);
	// 		// console.log(boxs[i].cli, boxs[i].getBoundingClientRect());
	// 		nowTop = boxs[i].getBoundingClientRect().top;
	// 		nowLeft = boxs[i].getBoundingClientRect().left;
	// 		nowWidth = boxs[i].getBoundingClientRect().width;
	// 		nowHeight = boxs[i].getBoundingClientRect().height;
	// 		if (boxs[i].className.split('site')[1] != 1 && boxs[i].className.split('site')[1] != 7 && boxs[i]
	// 			.className.split('site')[1] != 13 && boxs[i].className.split('site')[1] != 19) {
	// 			// console.log(parseInt(nowTop) - parseInt(nowHeight));
	// 			ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 25 + 'px';
	// 			ele.nowsite.style.left = parseInt(nowLeft) + 14 + 'px';
	// 		} else {
	// 			ele.nowsite.style.top = parseInt(nowTop) - parseInt(nowHeight) - 15 + 'px';
	// 			ele.nowsite.style.left = parseInt(nowLeft) + 24 + 'px';
	// 		}

	// 	}
	// }
	
	//打开 获奖记录
	ele.record.onclick = () => {
		ele.modal_record.style.display = 'block'
	}
	//关闭 获奖记录
	ele.close_record.onclick = () => {
		ele.modal_record.style.display = 'none'
	}
	
	var dt = 365 * 24 * 60 * 60;
	
	landlordIndex();
	function landlordIndex() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.LANDLORD_INDEX,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					jia = res.data.detail.bu;
					ele.todayDiceNum.innerHTML = res.data.detail.sz_num;
					ele.luxury.innerHTML = res.data.detail.luxury;
					ele.haohuaNum.innerHTML = res.data.detail.luxury;
					ele.myDice.innerHTML = res.data.detail.has_sz_num;
					ele.myJifen.innerHTML = res.data.detail.jf_num;
					setCookie("jifen", res.data.detail.jf_num,dt);
					setTimeout(()=> {
						oldposition(res.data.detail.bu);
					},200)
					drawPropList(res.data.list)
				}
				else openTips(res.msg || '未知错误');
			}
		});
	}
	
	// 渲染领取骰子列表 领取
	function drawPropList(data) {
		let html = '';
		for (let i = 0; i < data.length; i++) {
			let lv = data[i];
			let status = lv.is_click ? 'received' : 'receive';
			let txt = lv.is_click == 1 ? '已领取' : '领取';
			html += '<div class="tipBox">' +
				'<div class="thingbox">' +
				'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/luck/touzi.png" alt="">' +
				'<span>' + lv.num + '个</span>' +
				'</div>' +
				'<p class="haohua3">' + lv.luxury + '豪华值</p>' +
				'<button class="' + status +'" data="' + lv.luxury + '">' + txt + '</button>' +
				'</div>'
		}
		ele.transferboxinfo.innerHTML = html;
		// 领取豪华值列表
		let receives = $('.receive');
		for(let i = 0; i < receives.length;i++) {
			receives[i].onclick = ()=> {
				let thisluxury = receives[i].getAttribute('data');
				landlordGetSz(thisluxury);
			}
		}
	}
	
	// ele.tuozi.onclick=()=> {
	// 	landlordGetBu();
	// }
	
	// 接口丢筛子
	function landlordGetBu() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.LANDLORD_GET_BU,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					ele.tuozi.src = 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/BrowserPreview_tmp.gif';
					var tis = null;
					let i = jia;
					let randomNum = res.data; 
					setTimeout(()=> {
						ele.tuozi.src = 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/'+res.data+'.png';
						tis = setInterval(()=> {
							position(i++);
							if(i >= jia + randomNum) {
								clearInterval(tis)
								// jia = jia + randomNum;
								landlordBlocking();
							}
						},400)
					},1100)
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function landlordBlocking() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.LANDLORD_BLOCKING,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					if(res.data.info.length != 0) {
						if (res.data.info.b_type) {
							if (res.data.info.b_type == 1) {
								ele.recieveimg.src = "http://img.qxgs.cc/tiantianlaishouzu/img/home/yinbi3.png";
							} else if (res.data.info.b_type == 2) {
								ele.recieveimg.src = "http://img.qxgs.cc/tiantianlaishouzu/img/home/jinbi3.png";
							}
							ele.recieveimg.className = "flexcolomimg1";
							ele.recievenum.innerHTML =
								"<span>" + res.data.num + res.data.info.name + "</span>";
						} else {
							ele.recieveimg.src = IMG_DOMAIN + res.data.info.logo;
							let pj_type =
								res.data.info.hammer == 6 ?
								"itemred" :
								res.data.info.hammer == 2 ?
								"itemgreen" :
								res.data.info.hammer == 3 ?
								"itemblue" :
								res.data.info.hammer == 4 ?
								"itempurple" :
								res.data.info.hammer == 5 ?
								"itemorange" :
								"";
							ele.recievenum.innerHTML =
								"<span>" +
								res.data.info.name +
								"x" +
								parseInt(res.data.num) +
								"</span>";
							ele.recieveimg.className = "flexcolomimg1 " + pj_type;
						}
						if (res.data.info.is_default == 3) {
							let pj_type =
								res.data.info.hammer == 6 ?
								"itemred" :
								res.data.info.hammer == 2 ?
								"flexcolog1" :
								res.data.info.hammer == 3 ?
								"itemblue" :
								res.data.info.hammer == 4 ?
								"flexcolog2" :
								res.data.info.hammer == 5 ?
								"flexcolog3" :
								"";
							let mz_type =
								res.data.info.hammer == 6 ?
								"itemred" :
								res.data.info.hammer == 2 ?
								"mingzi1" :
								res.data.info.hammer == 3 ?
								"itemblue" :
								res.data.info.hammer == 4 ?
								"mingzi2" :
								res.data.info.hammer == 5 ?
								"mingzi3" :
								"";
							ele.floimg.style.display = "block";
							ele.recievenum.style.display = "none";
							ele.recieveimg.style.display = "none";
							ele.recieveimg2.className = pj_type;
							ele.recieveimg2.src = IMG_DOMAIN + res.data.info.rc_info.logo;
							ele.mingzi.innerHTML =
								"<p>" +
								res.data.info.rc_info.class.jz_name +
								'</p><span class="' +
								mz_type +
								'">' +
								res.data.info.rc_info.name +
								"</span>";
						} else {
							ele.floimg.style.display = "none";
							ele.recievenum.style.display = "block";
							ele.recieveimg.style.display = "block";
						}
						openMdl(ele.modal_tips2);
					} else {
						openTips('很遗憾，这一步啥也没有获得！')
					}
					landlordIndex();
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	function landlordGetSz(luxury) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.LANDLORD_GET_SZ,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				luxury: luxury
			},			
			success: res => {
				if (res.code == 1) {
					openTips(res.msg);
					landlordIndex();
					// ele.tuozi.src = 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/BrowserPreview_tmp.gif';
					// setTimeout(()=> {
					// 	ele.tuozi.src = 'http://img.qxgs.cc/tiantianlaishouzu/img/luck/1.png';
					// },1000)
				} else openTips(res.msg || '未知错误');
			}
		});
	}
	
	
	
	
	
	
	// 获奖记录列表
	getDetail(page);
	function getDetail(page) {
		ele.more.innerText = '加载中...';
		let token = getCookie("token");
		ajax({
			url: API_PATH.LANDLORD_RECORD,
			header: {
				'Authorization': token,
			},
			data: {
				page: page
			},
			success: res => {
				if (res.code == 1) {
					let detail = res.data.data
					if (detail.length === 0) {
						page = 0;
						ele.more.style.display = "none";
						ele.empty.style.display = "block";
					} else {
						drawDetail(detail);
						page++;
						if (page > res.data.last_page) {
							ele.more.innerText = '没有更多了';
							ele.more.onclick = null;
						}
						else {
							ele.more.innerText = '加载更多';
							ele.more.onclick = () => getDetail(page);
						}
					}
				}
				else openTips(res.msg);
			}
		});
	}
	// 渲染获奖记录
	function drawDetail(data) {
		if (data.length === 0) {
			page = 0;
			ele.more.style.display = "none";
			ele.empty.style.display = "block";
		} else {
			let str = '';
			for (let i = 0; i < data.length; i++) {
				let resume = data[i].remark;
				let sign = data[i].operation_type == 1 ? '+' : '-';
				let pj_type = data[i].house_info.hammer == 6 ? 'itemred' :  data[i].house_info.hammer == 2 ? 'itemgreen' : data[i].house_info.hammer == 3 ? 'itemblue' :  data[i].house_info.hammer == 4 ? 'itempurple' : data[i].house_info.hammer == 5 ? 'itemorange' : '';
				str += '<div class="tipBox">' +
					'<div class="tipin">' +
					'<div class="thingbox '+pj_type+'">' +
					'<img src="'+ IMG_DOMAIN + data[i].house_info.logo+'" alt="">' +
					'</div>' +
					'<p class="haohua haohua2">' + data[i].house_info.name+'x' +data[i].money + '</p>' +
					'</div>' +
					'<span class="time">' + data[i].created_at + '</span>' +
					'</div>';
			}
			ele.recordDataList.innerHTML += str;
		}
	}
	
	
	// 打开领取骰子
	ele.getDice.onclick = () => {
		ele.modal_transfer.style.display = 'block'
	}
	// 关闭领取骰子
	ele.close_transfer.onclick = () => {
		ele.modal_transfer.style.display = 'none'
	}

	
	// 绑定领取骰子按钮
	function bindReceive() {
		let btns = $('.receive');
		for (let i = 0; i < btns.length; i++) {
			btns[i].onclick = function () {
				receiveReward(this.getAttribute('data'));
			}
		}
		let btnds = $('.received');
		for (let i = 0; i < btnds.length; i++) {
			btnds[i].onclick = function () {
				openTips('您已领取过该奖励');
			}
		}
	}
	// 领取骰子
	function receiveReward(lv) {
		let token = getCookie("token");
		let type = getClientType();
		ajax({
			url: API_PATH.GOLD_EXCHANGE,
			method: 'POST',
			data: {
				id: lv,
				phone_type: type
			},
			header: {
				Authorization: token
			},
			success: res => {
				openTips(res.msg);
				if (res.code == 1) {
	
					let btn = $('.receive-' + lv)[0];
					btn.innerHTML = "已领取"
					btn.classList.remove('receive');
					btn.classList.add('received');
					btn.onclick = () => {
						openTips('您已领取过该奖励');
					};
				}
			}
		})
	}
	
})()